﻿using System.Windows.Forms;
using Com.OpenCode.Framework.Core;
using DevExpress.XtraBars.Docking;
using DevExpress.XtraEditors;

namespace Com.OpenCode.Framework.DevForm
{
    public class DockPanelUI : DockPanel, IDockPanel
    {
        private BuildItemEventArgs _BuildItemEventArgs;
        private Control _UserControl;

        public DockPanelUI(BuildItemEventArgs args)
        {
            args.Plugin.Owner = this;
            _BuildItemEventArgs = args;
            Options.AllowDockAsTabbedDocument = false;
            Text = args.Plugin.Properties.Get("Text").ToString();
            CreateControl();
            string dock = args.Plugin.Properties.Get("Dock").ToString();
            Dock = GetPanelDockArea(dock);
            (_BuildItemEventArgs.Owner as DockManagerUI).AddPanel(Dock, this);
            SetWidth(args.Plugin.Properties.Get("Width").ToString());
            SetDockArea();
            bool _CanClose = true;
            bool.TryParse(args.Plugin.Properties.Get("CanClose").ToString(), out _CanClose);
            Options.ShowCloseButton = _CanClose;
            bool _AllowFloat = false;
            bool.TryParse(args.Plugin.Properties.Get("AllowFloat").ToString(), out _AllowFloat);
            Options.AllowFloating = _AllowFloat;
            ClosedPanel += DockPanelUI_ClosedPanel;
        }
        public virtual Control UserControl
        {
            get { return _UserControl; } 
        }

        public void ShowPanel()
        {
            Show();
        }

        private void DockPanelUI_ClosedPanel(object sender, DockPanelEventArgs e)
        {
            _BuildItemEventArgs.Plugin.Owner = null;
        }

        protected new virtual void CreateControl()
        {
            if (_BuildItemEventArgs.Plugin.Properties.Exist("Class"))
            {
                _UserControl = (UserControl)_BuildItemEventArgs.Plugin.Addin.Runtime.CreateObject(_BuildItemEventArgs.Plugin.Properties.GetString("Class"));
            }
            else
            {
                _UserControl = (UserControl)ClassDoozer.BulidItem(_BuildItemEventArgs.Plugin.Addin, _BuildItemEventArgs.Plugin.Properties.GetString("ClassId"));
            }
            if (_UserControl == null)
            {
                _UserControl = new XtraUserControl();
            }
            ControlContainer container = new ControlContainer();
            container.Dock = DockStyle.Fill;
            container.Controls.Add(_UserControl);
            Controls.Add(container);
            _UserControl.Dock = DockStyle.Fill;
        }

        protected virtual void SetWidth(string width)
        {
            int _Width = 200;
            int.TryParse(width, out _Width);
            if (Dock == DockingStyle.Left || Dock == DockingStyle.Right)
            {
                Width = _Width;
            }
            else
            {
                Height = _Width;
            }
        }

        /// <summary>
        ///     获取停靠样式
        /// </summary>
        /// <param name="dock"></param>
        /// <returns></returns>
        public static DockingStyle GetPanelDockArea(string dock)
        {
            DockingStyle style = DockingStyle.Left;
            switch (dock)
            {
                case "Right":
                    style = DockingStyle.Right;
                    break;
                case "Top":
                    style = DockingStyle.Top;
                    break;
                case "Bottom":
                    style = DockingStyle.Bottom;
                    break;
                case "Fill":
                    style = DockingStyle.Fill;
                    break;
                case "Float":
                    style = DockingStyle.Float;
                    break;
            }
            return style;
        }

        public virtual void SetDockArea()
        {
            DockManagerUI _DockManagerUI = _BuildItemEventArgs.Owner as DockManagerUI;
            for (int i = 0; i < _DockManagerUI.RootPanels.Count; i++)
            {
                if (_DockManagerUI.RootPanels[i].Dock == Dock)
                {
                    DockAsTab(_DockManagerUI.RootPanels[i]);
                    break;
                }
            }
        }
    }
}